package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Question;
import cn.wolfcode.trip.base.mapper.AnswerMapper;
import cn.wolfcode.trip.base.mapper.QuestionMapper;
import cn.wolfcode.trip.base.query.QuestionQueryObject;
import cn.wolfcode.trip.base.service.IAnswerService;
import cn.wolfcode.trip.base.service.IQuestionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionServiceImpl implements IQuestionService {


    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private IAnswerService answerService;

    @Override
    public void saveOrUpdate(Question question) {
        if (question.getId() != null) {
            questionMapper.updateByPrimaryKey(question);
        } else {
            questionMapper.insert(question);
        }
    }

    @Override
    public void delete(Long id) {
        if (id != null) {
            questionMapper.deleteRelationByQuestionId(id);
            questionMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public List<Question> listByUserId(Long userId) {
        return questionMapper.selectByUserId(userId);
    }

    @Override
    public PageInfo<Question> query(QuestionQueryObject qo) {
        qo.setOrderBy("askTime DESC");
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());

        List<Question> list = questionMapper.queryForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public Question get(Long id) {
        return questionMapper.selectByPrimaryKey(id);
    }
}
